package com.erb.controller.ljy;

import com.erb.common.SignConstant;
import com.erb.entity.User;
import com.erb.entity.integral;
import com.erb.entity.signCalc;
import com.erb.entity.signDetail;
import com.erb.service.ljy.integralService;
import com.erb.service.ljy.signService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * ClassName: pointController
 * Description:签到的控制器
 * date: 2020/6/16 15:42
 *
 * @author JY
 */

/**
 * 每签到一次积分+5，
 * 连续签到7天：可领取礼包（10积分）
 * 连续签到15天: 可领取礼包（15积分）
 * 连续签到30天: 可领取礼包（30积分）
 */

@RestController
public class signController {


	@Resource
	private signService signService;

	@Resource
	private integralService integralService;

	public static void main(String[] args) {
		//        获取今天的日期
//        Map<String, Object> data = new HashMap<>();
//        Date date = new Date();
//        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
//        String today = simpleDateFormat.format(date);
//
////      获取昨天的日期
//        Calendar calendar = Calendar.getInstance();
//        calendar.add(Calendar.DATE, -1);
//        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
//        String yestday = sdf.format(calendar.getTime());
//
//        signDetail signDetail = signService.todayDeatil(today, userId);
//        signDetail signDetail1 = signService.yestDeatil(yestday, userId);
//        System.out.println("今天信息：" + signDetail.toString());
//        System.out.println("昨天信息：" + signDetail1.toString());


//        Calendar a = Calendar.getInstance();
//        a.set(Calendar.DATE, 1);//把日期设置为当月第一天
//        a.roll(Calendar.DATE, -1);//日期回滚一天，也就是最后一天
////        每月最后一个日期
//        int maxDate = a.get(Calendar.DATE);
//
//        System.out.println(maxDate);
//        Integer getsignCalc = signService.getcDays(userId);
//        System.out.println(getsignCalc);
	}

	/**
	 * Description: 签到接口
	 *
	 * @param :用户的ID，
	 * @author: JY
	 * @date: 2020/6/16 20:39
	 * @return:
	 */

	@RequestMapping("doSign")
	public Map<String, Object> doSign(HttpSession session) {
		User user = (User) session.getAttribute("user");
		Integer userId = user.getId();

//        获取今天的日期
		Map<String, Object> data = new HashMap<>();
		Date date = new Date();
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
		String today = simpleDateFormat.format(date);

//      获取昨天的日期
		Calendar calendar = Calendar.getInstance();
		calendar.add(Calendar.DATE, -1);
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String yestday = sdf.format(calendar.getTime());

//        if (user.getId() == userId && userId != null) {
//         查询今日是否签到
		signDetail signToday = signService.todayDeatil(today, userId);
		if (signToday == null) {//如果今日未签到
//                昨天签到信息
			signDetail signyestday = signService.yestDeatil(yestday, userId);
//            获取用户签到的天数
			Integer getsignCalc = signService.getcDays(userId);
			//如果昨天未签到
			if (signyestday == null) {
				signDetail signDetail = new signDetail();
				signDetail.setUserId(userId);
//                    signDetail.setSignDate(new Date());
//                添加到签到详情表
				signService.addsDetail(signDetail);
//                昨日未签到，已签到次数也为0的时候
				if (getsignCalc == null) {
					signCalc signCalc = new signCalc();
					signCalc.setUserId(userId);
					signCalc.setContinueDays(1);
					signService.addsCalc(signCalc);
				} else {
//                    昨天未签到但是已签到天数不为0，也是取消连续，修改连续签到数据为1
					signCalc signCalc = new signCalc();
					signCalc.setUserId(userId);
					signCalc.setContinueDays(1);
					signService.updateSignCalc(signCalc);
				}
//                添加积分信息
				integral inte = new integral();
				inte.setUserId(userId);
				inte.setScoreCount(SignConstant.INTEGRAL_TYPE_SIGN);
				inte.setCreateTime(new Date());
				integralService.addIntegral(inte);
//                给用户表中的字段添加积分
				integralService.addIntegralByUser(userId, SignConstant.NORMAL_SIGN_INTEGRAL_SCORES);
				data.put("code", "1");
				data.put("data", "签到成功");
			} else {//昨天已签到
				signDetail signDetail = new signDetail();
				signDetail.setUserId(userId);
				signDetail.setSignDate(new Date());
//                添加到签到详情表
				signService.addsDetail(signDetail);

				signCalc signCalc = new signCalc();
				signCalc.setUserId(userId);
//                原有的基础上加一
				signCalc.setContinueDays(signService.getcDays(userId) + 1);
//                修改连续签到天数
				signService.updateSignCalc(signCalc);
//                添加积分记录
				integral inte = new integral();
				inte.setUserId(userId);
				inte.setScoreCount(SignConstant.INTEGRAL_TYPE_SIGN);
//                    inte.setCreateTime(new Date());
				integralService.addIntegral(inte);
//                给用户表中的字段添加积分
				integralService.addIntegralByUser(userId, SignConstant.NORMAL_SIGN_INTEGRAL_SCORES);
				data.put("code", "1");
				data.put("data", "签到成功");
			}
		} else {
			data.put("code", "0");
			data.put("data", "今日已签到");
		}
//        }
//        else {
//            data.put("code", "0");
//            data.put("data", "maybe login error!");
//        }
		return data;
	}


//    @RequestMapping("test")
//    public String addintegral(Integer userId) {
//        Integer getsignCalc = signService.getcDays(userId);
//        System.out.println(getsignCalc);
//        return "查询成功";
//    }

	/**
	 * Description: 领取连续签到奖励接口
	 *
	 * @param :userId,连续签到对应天数
	 * @author: JY
	 * @date: 2020/6/17 14:11
	 */

	@RequestMapping("getGift")
	public Map<String, Object> getGift(Integer userId, Integer giftType) {
		HashMap<String, Object> data = new HashMap<>();
//       当日日期
		Date date = new Date();
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
		String today = simpleDateFormat.format(date);
//
		Calendar a = Calendar.getInstance();
		a.set(Calendar.DATE, 1);//把日期设置为当月第一天
		a.roll(Calendar.DATE, -1);//日期回滚一天，也就是最后一天
//        每月最后一个日期
		int maxDate = a.get(Calendar.DATE);
		// 获取用户签到的天数
		Integer getsignCalc = signService.getcDays(userId);
//       根据用户id,今日时间，分数记录查询今日是否已经领取过红包
		Integer signSeven = integralService.integralQuery(userId, SignConstant.INTEGRAL_TYPE_SEVEN_SIGN, today);
		Integer signFifteen = integralService.integralQuery(userId, SignConstant.INTEGRAL_TYPE_FIFTEEN_SIGN, today);
		Integer signThirty = integralService.integralQuery(userId, SignConstant.INTEGRAL_TYPE__THIRTY_SIGN, today);

//        判断领取哪个红包
//        七天领取
		if (signSeven == 0 && getsignCalc >= 7 && giftType == 7) {
			integral inte = new integral();
			inte.setUserId(userId);
			inte.setScoreCount(SignConstant.INTEGRAL_TYPE_SEVEN_SIGN);
//            添加积分记录
			integralService.addIntegral(inte);
			//                给用户表中的字段添加积分
			integralService.addIntegralByUser(userId, SignConstant.NORMAL_SEVEN_SIGN_INTEGRAL_SCORES);
			data.put("code", "1");
			data.put("data", "7日领取成功");
//            14天领取
		} else if (signFifteen == 0 && getsignCalc >= 15 && giftType == 15) {
			integral inte = new integral();
			inte.setUserId(userId);
			inte.setScoreCount(SignConstant.INTEGRAL_TYPE_FIFTEEN_SIGN);
//            添加积分记录
			integralService.addIntegral(inte);
			//                给用户表中的字段添加积分
			integralService.addIntegralByUser(userId, SignConstant.NORMAL_FIFTEEN_SIGN_INTEGRAL_SCORES);
			data.put("code", "1");
			data.put("data", "15日领取成功");
		} else if (signThirty == 0 && giftType == 30 && getsignCalc >= maxDate) {
			integral inte = new integral();
			inte.setUserId(userId);
			inte.setScoreCount(SignConstant.INTEGRAL_TYPE__THIRTY_SIGN);
//            添加积分记录
			integralService.addIntegral(inte);
			//                给用户表中的字段添加积分
			integralService.addIntegralByUser(userId, SignConstant.NORMAL_THIRTY_SIGN_INTEGRAL_SCORES);
			data.put("code", "1");
			data.put("data", "当月领取成功");
		} else {
			data.put("code", "0");
			data.put("data", "已领取或者未达标");
		}
		return data;
	}
}
